package com.weixin.web;

import com.agent.entity.CustomerSatisfaction;
import com.agent.entity.RepaireInfo;
import com.agent.entity.User;
import com.agent.service.ICustomerSatisfactionService;
import com.agent.service.IRepaireInfoService;
import com.agent.spring.websocket.SpringWebSocketHandler;
import com.agent.util.CommonUtils;
import com.agent.util.ResponseData;
import com.agent.vo.CustomerSatisfactionVO;
import com.agent.vo.RepaireInfoVO;
import com.weixin.pojo.Text;
import com.weixin.service.WeixinNotifyService;
import com.agent.service.UserService;
import com.agent.util.Constants;
import com.agent.util.ResponseData;
import com.agent.vo.CustomerSatisfactionVO;
import com.weixin.pojo.Textcard;
import com.weixin.pojo.WXMsg;
import com.weixin.service.WeixinNotifyService;
import com.weixin.util.WxepConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.TextMessage;
import sun.nio.ch.Util;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/weixin/customerSatisfaction/")
@Controller(value = "customerSatisfactionControllerForWX")
@Slf4j
public class CustomerSatisfactionController {
    @Resource(name = "customerSatisfactionService")
    ICustomerSatisfactionService service;

    @Resource(name = "repaireInfoService")
    IRepaireInfoService repaireInfoService;

    @Resource(name = "userServiceImpl")
    UserService userService;

    @Resource(name = "weixinNotifyService")
    WeixinNotifyService weixinNotifyService;

    @Bean
    public SpringWebSocketHandler infoHandler() {
        return new SpringWebSocketHandler();
    }

    @RequestMapping(value = "add" ,method = RequestMethod.POST)
    @ResponseBody
    public Map add(CustomerSatisfactionVO vo){
        if(!verifyStatus(vo)){
            return ResponseData.getErrorResult("该工单此时不能评价");
        }
        RepaireInfo repaireInfo = new RepaireInfo();
        repaireInfo.setWorkOrderId(vo.getWorkOrderId());
        repaireInfo.setStatus("05");
        repaireInfoService.updateByWorkOrderId(repaireInfo);
        CustomerSatisfaction customerSatisfaction = new CustomerSatisfaction();
        try {
            BeanUtils.copyProperties(vo,customerSatisfaction);
            service.insert(customerSatisfaction);
        } catch (Exception e) {
            log.error("com.weixin.web.CustomerSatisfactionController.add error | ",e);
            return ResponseData.getErrorResult("新增失败");
        }
        boolean satisfaction=false;//满意度
        Integer attitude=5,quality=5;
        if(!CommonUtils.isEmptyOrNull(vo.getQualityOfService()) && !CommonUtils.isEmptyOrNull(vo.getServiceAttitude())){
            attitude=Integer.valueOf(vo.getServiceAttitude());
            quality=Integer.valueOf(vo.getQualityOfService());
        }
        if(attitude<3 || quality<3){
            satisfaction=true;
        }
        if(satisfaction){ // 不满意  发送消息给班长座席
            assemblyAndNotifyMsg(vo.getWorkOrderId());
        }
        /*RepaireInfo repaireInfoParam = new RepaireInfo();
        repaireInfoParam.setWorkOrderId(vo.getWorkOrderId());
        List<RepaireInfo> list = repaireInfoService.getRepaireInfoList(repaireInfoParam);
        if(list !=null && list.size()>0){
            try {
                weixinNotifyService.sendRedPack(list.get(0).getCreator());
            } catch (Exception e) {
                log.error("com.weixin.web.CustomerSatisfactionController.sendRedPack error | ",e);
                return ResponseData.getErrorResult("发送红包失败");
            }
        }*/
        return ResponseData.getSuccessResult();
    }

    private boolean verifyStatus(CustomerSatisfactionVO vo){
        RepaireInfoVO vo1 = new RepaireInfoVO();
        vo1.setWorkOrderId(vo.getWorkOrderId());
        List<RepaireInfo> repaireInfoList = repaireInfoService.getRepaireInfoList(vo1);
        if(!CommonUtils.isEmptyOrNull(repaireInfoList)){
            RepaireInfo repaireInfo = repaireInfoList.get(0);
            if(repaireInfo.getStatus().equals("04")){ // 服务完成待评价
                return true;
            }
        }
        return false;
    }
    public void assemblyAndNotifyMsg(String workOrderId){
        /*User in = new User();
        in.setRoleId(2);*/
        List<User> users = userService.findByRole();
        if(users ==null || users.size()==0){
            return;
        }
        StringBuffer toUser = new StringBuffer();
        users.forEach(user ->{
            toUser.append(user.getUserName());
            toUser.append("|");
        });
        WXMsg wxMsg = new WXMsg();
        wxMsg.setTouser(toUser.substring(0,toUser.length()-1));
        wxMsg.setAgentid(WxepConstants.CUSTOMER_AGENT);
        wxMsg.setMsgtype("text");
        Text text = new Text();
        text.setContent("有工单反馈不满意 工单号为："+workOrderId);
        String tips = "有工单反馈不满意 工单号为：<a href=javascript:show(\""+workOrderId+"\")>"+workOrderId+"</a>";
//        textcard.setUrl(WxepConstants.ENGINEER_MODIFY_URL);
        wxMsg.setText(text);
        //推送web页面
        try {
            // TODO 后续坐席服务添加上之后 放开  2019-07-23
//            weixinNotifyService.sendMsg(wxMsg,WxepConstants.CUSTOMER_SECRET);
            infoHandler().sendMessageToUsersByName(toUser.toString(),new TextMessage(tips));
        } catch (Exception e) {
            log.error("com.weixin.web.CustomerSatisfactionController.assemblyAndNotifyMsg error | ",e);
        }

    }

}
